package com.ruoyi.system.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.PageConfig;
import com.ruoyi.system.service.IPageConfigService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 页面配置Controller
 * 
 * @author ruoyi
 * @date 2025-03-30
 */
@RestController
@RequestMapping("/system/pageConfig")
public class PageConfigController extends BaseController
{
    @Autowired
    private IPageConfigService pageConfigService;

    /**
     * 查询页面配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:pageConfig:list')")
    @GetMapping("/list")
    public TableDataInfo list(PageConfig pageConfig)
    {
        startPage();
        List<PageConfig> list = pageConfigService.selectPageConfigList(pageConfig);
        return getDataTable(list);
    }

    /**
     * 导出页面配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:pageConfig:export')")
    @Log(title = "页面配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PageConfig pageConfig)
    {
        List<PageConfig> list = pageConfigService.selectPageConfigList(pageConfig);
        ExcelUtil<PageConfig> util = new ExcelUtil<PageConfig>(PageConfig.class);
        util.exportExcel(response, list, "页面配置数据");
    }

    /**
     * 获取页面配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:pageConfig:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pageConfigService.selectPageConfigById(id));
    }

    /**
     * 新增页面配置
     */
    @PreAuthorize("@ss.hasPermi('system:pageConfig:add')")
    @Log(title = "页面配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PageConfig pageConfig)
    {
        return toAjax(pageConfigService.insertPageConfig(pageConfig));
    }

    /**
     * 修改页面配置
     */
    @PreAuthorize("@ss.hasPermi('system:pageConfig:edit')")
    @Log(title = "页面配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PageConfig pageConfig)
    {
        return toAjax(pageConfigService.updatePageConfig(pageConfig));
    }

    /**
     * 删除页面配置
     */
    @PreAuthorize("@ss.hasPermi('system:pageConfig:remove')")
    @Log(title = "页面配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pageConfigService.deletePageConfigByIds(ids));
    }

    /**
     * 发布页面配置
     */
    @PreAuthorize("@ss.hasPermi('system:pageConfig:edit')")
    @Log(title = "页面配置", businessType = BusinessType.UPDATE)
    @PostMapping("/publish/{id}")
    public AjaxResult publish(@PathVariable("id") Long id)
    {
        return toAjax(pageConfigService.publishPageConfig(id));
    }
    
    /**
     * 取消发布页面配置
     */
    @PreAuthorize("@ss.hasPermi('system:pageConfig:edit')")
    @Log(title = "页面配置", businessType = BusinessType.UPDATE)
    @PostMapping("/unpublish/{id}")
    public AjaxResult unpublish(@PathVariable("id") Long id)
    {
        return toAjax(pageConfigService.unpublishPageConfig(id));
    }
    
    /**
     * 获取页面版本历史
     */
    @PreAuthorize("@ss.hasPermi('system:pageConfig:query')")
    @GetMapping("/version/{id}")
    public AjaxResult getVersionHistory(@PathVariable("id") Long id)
    {
        List<Map<String, Object>> versionList = pageConfigService.getPageVersionHistory(id);
        return success(versionList);
    }
}
